<?php

//include icon-class
include("includes/icons/icon-inc.php");

// bytes to human readable class
class resize_bytes{
	var $size;
	var $human_readable;
	function resize_bytes($size)
	{
		$this->size = $size;
		$count = 0;
		if($size > 1){
			$format = array("Bytes","KB","MB","GB");
		} else {
			$format = array("Byte","KB","MB","GB");
		}
		while(($size/1024)>1 && $count<3)
		{
			$size=$size/1024;
			$count++;
		}
		$decimals = 0;
		$this->human_readable = number_format((double)$size,$decimals,'','.')." ".$format[$count];
		return $this->human_readable;
	}
}

class list_dirs_size {
    var $dirsize;
    function list_dirs_size($path,$option) {
          if($option == TRUE){
          	chdir($path);
          	$currpath = getcwd();
            if($handle = opendir($path)){
                while(FALSE !== ($object = readdir($handle))){
                    if($object != "." && $object != ".."){
                		$actualpath = getcwd()."/".$object;
                        if(is_link($object)){
                            $this->dirsize += 0;
                        }elseif(is_dir($actualpath)){
                        	chdir($actualpath);
                            $this->list_dirs_size($actualpath,$option);
                        }else{
                            $dirsize_tmp = stat($path."/".$object);
                            $this->dirsize += $dirsize_tmp['size'];
                        }
                    }
                }
            }
        }elseif($option == FALSE){
            $this->dirsize = "&nbsp;";
        }else{
            $this->error[] = "Show recursive dirsize? Use option: TRUE or FALSE";
        }
        return $this->dirsize;
    }
}

// directory and file lister (with stats/permissions)
class list_dirs{
    var $dirs;
    var $dirs_stats;
    var $files;
    var $files_stats;
    function list_dirs($path,$showhidden){
        if($handle = @opendir($path)){
            while(FALSE !== ($object = readdir($handle))){
                if($object != "." && $object != ".."){
                    switch($showhidden){
                        case 0:
                            if(is_dir($path.$object) && $object{0} != "."){
                                $this->dirs[] = $object;
                                $this->dirs_stats[] = stat($path.$object);
                                $this->permissions_dirs[] = fileperms($path.$object);
                            }elseif($object{0} != "."){
                                $this->files[] = $object;
                                $this->files_stats[] = stat($path.$object);
                                $this->permissions_files[] = fileperms($path.$object);
                            }
                        break;
                        case 1:
                            if(is_dir($path.$object)){
                                $this->dirs[] = $object;
                                $this->dirs_stats[] = stat($path.$object);
                                $this->permissions_dirs[] = fileperms($path.$object);
                            }else{
                                $this->files[] = $object;
                                $this->files_stats[] = stat($path.$object);
                                $this->permissions_files[] = fileperms($path.$object);
                            }
                        break;
                        default:
                            $this->error[] = "Show hidden files? Use option: 1 or 0";
                    }
                }
            }
        }
    }
}

// get file/directory permissions
class get_perm{
    var $info;
    function get_perm($permissions,$permtype){
        if ($permtype == 1){
            $this->get_perm_full($permissions);
        }elseif ($permtype == 2){
            $this->get_perm_short($permissions);
        }elseif ($permtype == 3){
            $this->info = "N/A";
        }
    }
    function get_perm_full($permissions){
        if (($permissions & 0xC000) == 0xC000) {
          // Socket
          $this->info = 's';
       } elseif (($permissions & 0xA000) == 0xA000) {
          // Symbolic Link
          $this->info = 'l';
       } elseif (($permissions & 0x8000) == 0x8000) {
          // Regular
          $this->info = '-';
       } elseif (($permissions & 0x6000) == 0x6000) {
          // Block special
          $this->info = 'b';
       } elseif (($permissions & 0x4000) == 0x4000) {
          // Directory
          $this->info = 'd';
       } elseif (($permissions & 0x2000) == 0x2000) {
          // Character special
          $this->info = 'c';
       } elseif (($permissions & 0x1000) == 0x1000) {
          // FIFO pipe
          $this->info = 'p';
       } else {
          // Unknown
          $this->info = 'u';
       }
       
       // Owner
       $this->info .= (($permissions & 0x0100) ? 'r' : '-');
       $this->info .= (($permissions & 0x0080) ? 'w' : '-');
       $this->info .= (($permissions & 0x0040) ?
                  (($permissions & 0x0800) ? 's' : 'x' ) :
                  (($permissions & 0x0800) ? 'S' : '-'));
       
       // Group
       $this->info .= (($permissions & 0x0020) ? 'r' : '-');
       $this->info .= (($permissions & 0x0010) ? 'w' : '-');
       $this->info .= (($permissions & 0x0008) ?
                  (($permissions & 0x0400) ? 's' : 'x' ) :
                  (($permissions & 0x0400) ? 'S' : '-'));
       
       // World
       $this->info .= (($permissions & 0x0004) ? 'r' : '-');
       $this->info .= (($permissions & 0x0002) ? 'w' : '-');
       $this->info .= (($permissions & 0x0001) ?
                  (($permissions & 0x0200) ? 't' : 'x' ) :
                  (($permissions & 0x0200) ? 'T' : '-'));
       return $this->info;
   }
    function get_perm_short($permissions){
        $this->info = substr(sprintf('%o', $permissions), -4);
        return $this->info;
   }
}

class remove_subdir {
	var $clean;
	// remove subdirectories and its files of the same superior directory, as it is stupid to add them
	function remove_subdir($arr1, $arr2, $type){
	    $clean_tmp = $arr1; // we need a copy of input array
	    $clean_tmp2 = $arr1; // we need a 2nd copy of input array
	    $clean_tmp3 = $arr2; // we need a 3rd copy of input array
        $clean_tmp4 = $arr2; // we need a 4th copy of input array
        if($type == "dirs"){
            // create all possible combinations
            for($i=0;$i<count($arr1);$i++){
                for($j=0;$j<count($arr1);$j++){
                    $length1 = strlen($arr1[$i]);
                    $length2 = strlen($arr1[$j]);
                    if($arr1[$i]!= $arr1[$j]){
                        if($length1>$length2){
                            $arr1_icut = substr($arr1[$i], 0, $length2);
                            if($arr1_icut == $arr1[$j]){
                                $tocut[] = $arr1[$i];
                            }
                        }elseif($length2>$length1){
                            $arr1_jcut = substr($arr1[$j], 0, $length1);
                            if($arr1_jcut == $arr1[$i]){
                                $tocut[] = $arr1[$j];
                            }
                        }elseif($length1 == $length2){
                            // do nothing, as equal is ok
                        }
                    }
                }
            }
        // do some fixup
        for($x=0;$x<count($clean_tmp);$x++){
            for($y=0;$y<count($tocut);$y++){
                if($clean_tmp[$x] == $tocut[$y]){
                    unset($clean_tmp2[$x]);
                }
            }
        }
        natsort($clean_tmp2);
        $cleancount = 0;
        foreach($clean_tmp2 as $cleanone){
            $cleaned[$cleancount] = $cleanone;
            $cleancount++;
        }
        if(count($cleaned)>0){
            $cleaned = array_unique($cleaned);
        }
        $this->clean = $cleaned;
        return $this->clean;
        
        }elseif($type == "files"){
            // create all possible combinations
            for($i=0;$i<count($arr1);$i++){
                for($j=0;$j<count($arr2);$j++){
                    $namedir = dirname($arr2[$j])."/";
                    if($namedir == "//"){$namedir = $arr1[$i];}
                    $length1 = strlen($arr1[$i]);
                    $namedir_cut = substr($namedir, 0, $length1);
                    if($arr1[$i] == $namedir_cut){
                        if(substr_count($arr2[$j], '/') != 1){
                            $tocut[] = $arr2[$j];
                        }
                    }
                }
            }
        }
        // do some fixup
        for($x=0;$x<count($clean_tmp3);$x++){
            for($y=0;$y<count($tocut);$y++){
                if($clean_tmp3[$x] == $tocut[$y]){
                    unset($clean_tmp4[$x]);
                }
            }
        }
        natsort($clean_tmp4);
        $cleancount = 0;
        foreach($clean_tmp4 as $cleanone){
            $cleaned[$cleancount] = $cleanone;
            $cleancount++;
        }
        if(count($cleaned)>0){
            $cleaned = array_unique($cleaned);
        }
        $this->clean = $cleaned;
        return $this->clean;
	}
}

class read_template {
    var $template;
    function read_template($tmpl) {
        $fp = fopen($tmpl, "r") or die ("<b style=\"color:rgb(255,0,0);\">". ERROR_NO_TEMPATE ." <i>$tmpl</i></b>\n");
        while($line = fgets($fp, 1024)){
            $this->template .= $line;
        }
        fclose($fp);
        return $this->template;
    }
}

class create_buttons {
    var $buttonxhtml;
    function make_button($basefile,$buttonimg,$alttext) {
       $this->buttonxhtml .= "<a href=\"".$basefile."\"><img src=\"".$buttonimg."\" alt=\"".$alttext."\" border=\"0\" class=\"linkrahmen\" /></a>&nbsp;\n";
       return $this->buttonxhtml;
    }
    function make_formbutton($buttonimg,$alttext,$value){
        $this->buttonxhtml .= "<input class=\"linkrahmen\" type=\"image\" src=\"".$buttonimg."\" alt=\"".$alttext."\" name=\"action\" value=\"".$value."\" />&nbsp;\n";
        return $this->buttonxhtml;
    }
    function make_spacer() {
        $this->buttonxhtml .= "<img src=\"img/trenn.gif\" width=\"10\" height=\"20\" alt=\"|\" border=\"0\" />&nbsp;\n";
        return $this->buttonxhtml;
    }
}

class listing {
    var $tablelisting;
    function listing($path,$option,$permtype,$getpath){
        // checking os for posix error when using windows server
        if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $this->os = 0;
        } else {
            $this->os = 1;
        }    
        $this->listing = new list_dirs($path,$option);
        $this->generate_table($listing,$permtype,$getpath);        
    }
    function generate_table($listing,$permtype,$getpath){
        $listing = $this->listing;
        if(count($listing->dirs)<1){
            $this->tablelisting .= "<tr>\n";
            $this->tablelisting .= "<td colspan=\"8\">".NODIRS."</td>";
            $this->tablelisting .= "</tr>\n";
        } else {
        	for($i=0;$i<count($listing->dirs);$i++){
                $foldsize = new list_dirs_size($abspath.$getpath.$listing->dirs[$i],$recursivedirs);
                $size = new resize_bytes($foldsize->dirsize);
                if($this->os == 1){
            	    $myuser = posix_getpwuid($listing->dirs_stats[$i]['uid']);
            	    $mygroup = posix_getgrgid($listing->dirs_stats[$i]['gid']);
            	}else {
            	    $myuser['name'] = "N/A";
            	    $mygroup['name'] = "N/A";
            	}
            	$dirdate = date("d-m-Y, H:i",$listing->dirs_stats[$i][mtime]);
            	$perm = new get_perm($listing->permissions_dirs[$i],$permtype);
        	    $this->tablelisting .= "<tr class=\"tr\">\n";
        	    $this->tablelisting .= "<td><input type=\"checkbox\" name=\"dirs[]\" value=\"/".$getpath.$listing->dirs[$i]."/\" /></td>\n";
        	    $this->tablelisting .= "<td><a href=\"index.php?path=".$getpath.$listing->dirs[$i]."/\"><img src=\"img/folder.gif\" alt=\"dir:".$listing->dirs[$i]."\" border=\"0\" /></a></td>\n";
        	    $this->tablelisting .= "<td><a href=\"index.php?path=".$getpath.$listing->dirs[$i]."/\">".$listing->dirs[$i]."</a></td>\n";
        	    $this->tablelisting .= "<td>".$size->human_readable."</td>\n";
        	    $this->tablelisting .= "<td>".$dirdate."</td>";
        	    $this->tablelisting .= "<td>".$perm->info."</td>\n";
        	    $this->tablelisting .= "<td>".$myuser['name']."</td>\n";
        	    $this->tablelisting .= "<td>".$mygroup['name']."</td>\n";
        	    $this->tablelisting .= "</tr>\n";
        	}
        }
        if(count($listing->files)<1){
            $this->tablelisting .= "<tr>\n";
            $this->tablelisting .= "<td colspan=\"8\">".NOFILS."</td>";
            $this->tablelisting .= "</tr>\n";
        } else {
            for($i=0;$i<count($listing->files);$i++){
                if($this->os == 1){
            	    $myuser = posix_getpwuid($listing->files_stats[$i]['uid']);
            	    $mygroup = posix_getgrgid($listing->files_stats[$i]['gid']);
            	}else {
            	    $myuser['name'] = "N/A";
            	    $mygroup['name'] = "N/A";
            	}
            	$filedate = date("d-m-Y, H:i",$listing->files_stats[$i]['mtime']);
                $size = new resize_bytes($listing->files_stats[$i]['size']);
            	$perm = new get_perm($listing->permissions_files[$i],$permtype);
        	    $this->tablelisting .= "<tr class=\"tr\">\n";
        	    $this->tablelisting .= "<td><input type=\"checkbox\" name=\"files[]\" value=\"/".$getpath.$listing->files[$i]."\" /></td>\n";
        	    $this->tablelisting .= "<td><a href=\"http://".$_SERVER['SERVER_NAME'].$relpath."/test/".$getpath.$listing->files[$i]."\"><img src=\"".icon_path("../../test/".$getpath.$listing->files[$i])."\" alt=\"file:".$listing->files[$i]."\" border=\"0\" /></a></td>\n";
        	    $this->tablelisting .= "<td><a href=\"http://".$_SERVER['SERVER_NAME'].$relpath."/test/".$getpath.$listing->files[$i]."\">".$listing->files[$i]."</a></td>\n";
        	    $this->tablelisting .= "<td>".$size->human_readable."</td>\n";
        	    $this->tablelisting .= "<td>".$filedate."</td>";
        	    $this->tablelisting .= "<td>".$perm->info."</td>\n";
        	    $this->tablelisting .= "<td>".$myuser['name']."</td>\n";
        	    $this->tablelisting .= "<td>".$mygroup['name']."</td>\n";
        	    $this->tablelisting .= "</tr>\n";
                $listing->filesumma = $listing->filesumma + $listing->files_stat[$i]['size'];
            }
        }
    return $this->tablelisting;
    }
}
?>
